# -*- coding: utf-8 -*-
# @Time : 2021/12/30 10:22
# @Author : Limusen
# @File : demo_selector_10

import time
import random
from selenium import webdriver
from selenium.webdriver.support.select import Select

driver = webdriver.Chrome()
driver.get("https://sahitest.com/demo/selectTest.htm")
driver.implicitly_wait(5)

# 定位到下拉框元素
selectElement = driver.find_element_by_xpath("//select[@id='s3Id']")
s = Select(selectElement)
s.select_by_index(2)  # 根据下标选择下拉框第三个元素
time.sleep(2)
s.select_by_value("o4val") # 元素的value值
time.sleep(2)
s.select_by_visible_text("o3") #　可见文本内容

# 定位到元素列表然后随机选择一个元素选择
time.sleep(2)
selectElement= driver.find_element_by_xpath("//select[@id='s1']")
time.sleep(2)
selectElement.click()
time.sleep(2)
elements = driver.find_elements_by_xpath("//select[@id='s1']/option")
time.sleep(2)
random.choice(elements).click()

